/**
 * 
 */
package com.lmm.test;

import java.net.URI;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.lmm.http.client.HttpClientUtils;
import com.lmm.http.client.HttpClientUtils.HttpMethod;

/**
 * @author leiming
 *
 */
public class ClientAuthentication {

    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("127.0.0.1", 8080),
                    new UsernamePasswordCredentials("leiming", "test"));
            //HttpGet httpget = new HttpGet("http://127.0.0.1:8080/test3/test3.jsp");
            HttpUriRequest httpget = HttpClientUtils.createHttpUriRequest(HttpMethod.toHttpMethod("get") , new URI("http://127.0.0.1:8080/test3/test3.jsp"));
            //HttpGet httpget = new HttpGet("http://stage.zt906.com/api/mobile/user.pfv"); 
            httpget.setHeader("Auth", "6YKT5LiJ5LiJOkY1OUJENjVGN0VEQUZCMDg3QTgxRDREQ0EwNkM0OTEw");
            //httpget.getParams().setParameter("name", new String[]{"leiming1","leiming2"});
            System.out.println("executing request" + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            Header headers[] =response.getAllHeaders();
            for(Header h :headers){
            	System.out.println("headerName:"+h.getName()+" headerValue:"+h.getValue());
            }
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            String content = EntityUtils.toString(entity, "utf-8");
            System.out.println(content);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}
